package leetcode_tag.树;

import leetcode.TreeNode;

import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class LeetCode144 {

    public static void main(String[] args) {
        LeetCode144 leetCode144 = new LeetCode144();
        leetCode144.preorderTraversal(new TreeNode(1));
    }

    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ans = new LinkedList<>();
        if (root == null) {
            return ans;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.add(root);
        while (!stack.isEmpty()) {
            TreeNode pop = stack.pop();
            if (pop != null) {
                ans.add(pop.val);
                stack.add(pop.right);
                stack.add(pop.left);
            }
        }
        return ans;
    }
}
